package com.ruoyi.web.controller.tlc.back;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.tlc.TlcFaq;
import com.ruoyi.system.domain.tlc.TlcMap;
import com.ruoyi.system.service.tlc.ITlcFaqService;
import com.ruoyi.system.service.tlc.ITlcMapService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 参数配置 信息操作处理
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/map")
public class TlcMapController extends BaseController
{
    @Autowired
    private ITlcMapService tlcMapService;

    @Log(title = "地图管理", businessType = BusinessType.LIST)
    @PreAuthorize("@ss.hasPermi('system:tlcmap:list')")
    @GetMapping("/list")
    public TableDataInfo list(TlcMap map)
    {
        startPage();
        LambdaQueryWrapper<TlcMap> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderBy(true,false,TlcMap::getCreateTime);
        queryWrapper.orderBy(true,false,TlcMap::getStatus);
        if (StringUtils.isNotBlank(map.getName())){
            queryWrapper.like(TlcMap::getName, map.getName());
        }
        if (CollUtil.isNotEmpty(map.getTypes())){
            queryWrapper.and(x->{
                for (String type : map.getTypes()) {
                    x.or().like(TlcMap::getType, String.format(",%s,",type));
                }
            });

        }
        if(map.getStatus()!=null){
            queryWrapper.eq(TlcMap::getStatus,map.getStatus());
        }
        //未删除的
        queryWrapper.eq(TlcMap::getDelFlag,0);
        List<TlcMap> list = tlcMapService.list(queryWrapper);
        for (TlcMap tlcMap : list) {
            tlcMap.setTypes(Arrays.asList(tlcMap.getType().split(",")));
        }
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:tlcmap:add')")
    @Log(title = "地图管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody TlcMap map)
    {
        String type = StringUtils.join(map.getTypes(),",");
        if(StringUtils.isNotBlank(type)){
            type = String.format(",%s,",type);
        }
        map.setType(type);
        map.setCreateBy(getUsername());
        map.setCreateTime(new Date());
        map.setDelFlag(0);
        map.setStatus(0);
        return toAjax(tlcMapService.save(map));
    }

    @PreAuthorize("@ss.hasPermi('system:tlcmap:update')")
    @Log(title = "地图管理", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody TlcMap map)
    {
        String type = StringUtils.join(map.getTypes(),",");
        if(StringUtils.isNotBlank(type)){
            type = String.format(",%s,",type);
        }
        map.setType(type);
        map.setUpdateBy(getUsername());
        map.setUpdateTime(new Date());
        return toAjax(tlcMapService.updateById(map));
    }

    @PreAuthorize("@ss.hasPermi('system:tlcmap:detail')")
    @GetMapping("/toUpdate/{id}")
    public AjaxResult toUpdate(@PathVariable Long id)
    {
        TlcMap map = tlcMapService.getById(id);
        map.setTypes(Arrays.asList(map.getType().split(",")));
        return AjaxResult.success(map);
    }

    @PreAuthorize("@ss.hasPermi('system:tlcmap:canuse')")
    @Log(title = "地图管理", businessType = BusinessType.UPDATE)
    @PostMapping("/canuse")
    public AjaxResult canuse(@RequestBody Long[] ids)
    {
        LambdaUpdateWrapper<TlcMap> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(TlcMap::getStatus,0).in(TlcMap::getId, ids);
        return toAjax(tlcMapService.update(new TlcMap(), wrapper));
    }

    @PreAuthorize("@ss.hasPermi('system:tlcmap:notuse')")
    @Log(title = "地图管理", businessType = BusinessType.UPDATE)
    @PostMapping("/notuse")
    public AjaxResult notuse(@RequestBody Long[] ids)
    {
        LambdaUpdateWrapper<TlcMap> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(TlcMap::getStatus,1).in(TlcMap::getId, ids);
        return toAjax(tlcMapService.update(new TlcMap(), wrapper));
    }

    @PreAuthorize("@ss.hasPermi('system:tlcmap:del')")
    @Log(title = "地图管理", businessType = BusinessType.DELETE)
    @PostMapping("/del")
    public AjaxResult del(@RequestBody Long[] ids)
    {
        LambdaUpdateWrapper<TlcMap> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(TlcMap::getDelFlag,1).in(TlcMap::getId, ids);
        return toAjax(tlcMapService.update(new TlcMap(), wrapper));
    }
}
